import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import { getMenus, updateMenus, deleteMenus } from '@/api/menus'
import store from '@/store'

export interface ISysMenuState {

}

@Module({ dynamic: true, store, name: 'SysMenu' })
class SysMenu extends VuexModule implements ISysMenuState {

    @Action
    public async getMenus(menuId?: number) {
        return new Promise((r: any, j: any) => {
            let data = menuId ? { menuId: menuId } : null
            getMenus(data).then((value: any) => {
                console.error("Menu:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateMenus(data: any) {
        return new Promise((r: any, j: any) => {
            updateMenus(data).then((value: any) => {
                console.error("Menu:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteMenus(menuId: any) {
        return new Promise((r: any, j: any) => {
            deleteMenus({ menuId: menuId }).then((value: any) => {
                console.error("Menu:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }
}

export const SysMenuModule = getModule(SysMenu)
